﻿//AcademicPlanner - course registration planning web app.
//Copyright (C) 2009  Boubacar Diallo

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


using System;
using System.Collections.Generic;
using System.Web.Mvc;
using AcademicPlanner.Domain.Models;

namespace AcademicPlanner.Presentation.Views.Shared
{
	/// <summary>
	/// Possible scenarios of a <c>CourseForm</c> form.
	/// </summary>
	/// <since>1.0</since>
	public enum CourseFormScenario
	{
		CREATE,
		EDIT
	}


	/// <summary>
	/// CourseFormModel represents a model object for a <c>CourseForm</c> form.
	/// </summary>
	/// <since>1.0</since>
	public class CourseFormModel : FormModel
	{
		/// <summary>
		/// Gets or sets the scenario to use the <c>CourseForm</c> form under.
		/// </summary>
		/// <since>1.0</since>
		public CourseFormScenario Scenario { get; set; }

		/// <summary>
		/// Gets or sets the <c>Course</c> whose data to save.
		/// </summary>
		/// <remarks>
		/// This property is not used in <c>CREATE</c> scenario.
		/// </remarks>
		/// <since>1.0</since>
		public Course Course { get; set; }

		/// <summary>
		/// Gets or sets the <c>AcademicPlan</c> the academic course belongs to.
		/// </summary>
		/// <remarks>
		/// This property is used in <c>CREATE</c> scenario only.
		/// </remarks>
		/// <since>1.0</since>
		public AcademicPlan AcademicPlan { get; set; }
	}


	/// <summary>
	/// CourseForm represents a form control to save a <c>Course</c> data.
	/// </summary>
	/// <since>1.0</since>
	public partial class CourseForm : Form<CourseFormModel>
	{
		/// <summary>
		/// Gets the model object for this form control.
		/// </summary>
		/// <since>1.0</since>
		public new CourseFormModel Model { get { return (CourseFormModel)base.Model; } }

		/// <summary>
		/// Gets or sets the model object for the <c>CoursesControl</c> control contained in this form
		/// for selection of prerequisite courses.
		/// </summary>
		/// <since>1.0</since>
		public CoursesControlModel SelectPrerequisiteCoursesModel { get; set; }

		/// <summary>
		/// Gets or sets the model object for the <c>AcademicTermsControl</c> control contained in this form
		/// for selection of academic terms an academic course might be commonly offered in.
		/// </summary>
		/// <since>1.0</since>
		public AcademicTermsControlModel SelectCommonlyOfferedInTermsModel { get; set; }

		/// <summary>
		/// Gets or sets the value of the credits field of this form.
		/// </summary>
		/// <since>1.0</since>
		public string CreditsValue { get; set; }

		/// <summary>
		/// Gets or sets the value of the name field of this form.
		/// </summary>
		/// <since>1.0</since>
		public string NameValue { get; set; }

		/// <summary>
		/// Gets or sets the value of the number field of this form.
		/// </summary>
		/// <since>1.0</since>
		public string NumberValue { get; set; }

		/// <summary>
		/// Gets or sets the list of options in the category dropdown of this form.
		/// </summary>
		/// <since>1.0</since>
		public IList<SelectListItem> CategoryOptions { get; set; }

		/// <summary>
		/// Gets or sets the value of the category field of this form.
		/// </summary>
		/// <since>1.0</since>
		public string CategoryValue { get; set; }


		/// <summary>
		/// Handles the page load event for this form.
		/// </summary>
		/// <remarks>
		/// This methods intializes or customizes the form's properties
		/// based on the model object's <c>Scenario</c>.
		/// </remarks>
		/// <param name="sender">The sender of the event.</param>
		/// <param name="e">The details about the event.</param>
		/// <since>1.0</since>
		protected void Page_Load(object sender, EventArgs e)
		{
			method = FormMethod.Post;

			if (Model.Scenario == CourseFormScenario.EDIT)
			{
				actionName = "Save";
				controllerName = "Courses";
				routeValues = new { pId = Model.Course.EntityId };
				Title = Tr("Edit Course");
				Name = "editCourseForm";
				SubmitButtonValue = Tr("Save");
			}
			else
			{
				actionName = "Save";
				controllerName = "Courses";
				routeValues = new { pId = 0, pParentId = Model.AcademicPlan.EntityId };
				Title = Tr("Create Course");
				Name = "createCourseForm";
				SubmitButtonValue = Tr("Save");
			}

			if (string.IsNullOrEmpty(Model.Id)) Model.Id = Name;

			SelectPrerequisiteCoursesModel = new CoursesControlModel();
			SelectCommonlyOfferedInTermsModel = new AcademicTermsControlModel();
			SelectPrerequisiteCoursesModel.Mode = CoursesControlMode.SELECT;
			SelectCommonlyOfferedInTermsModel.Mode = AcademicTermsControlMode.SELECT;
			SelectPrerequisiteCoursesModel.Id = Model.Id + "Prerequisites";
			SelectCommonlyOfferedInTermsModel.Id = Model.Id + "CommonlyOfferedInTerms";
			SelectPrerequisiteCoursesModel.SelectedCoursesFieldName = GetFieldName("Prerequisites");
			SelectCommonlyOfferedInTermsModel.SelectedAcademicTermsFieldName = GetFieldName("CommonlyOfferedInTerms");
			SelectPrerequisiteCoursesModel.Courses = Model.AcademicPlan.Courses.ToList();
			SelectCommonlyOfferedInTermsModel.AcademicTerms = Model.AcademicPlan.Terms.ToList();

			if (Model.Scenario == CourseFormScenario.EDIT)
			{
				NameValue = Model.Course.Name;
				CreditsValue = Model.Course.Credits.ToString();
				NumberValue = Model.Course.Number.ToString();
				CategoryValue = Model.Course.Category.EntityId.ToString();
				SelectPrerequisiteCoursesModel.SelectedCourses = Model.Course.Prerequisites.ToList();
				SelectCommonlyOfferedInTermsModel.SelectedAcademicTerms = Model.Course.CommonlyOfferedInTerms.ToList();
				SelectPrerequisiteCoursesModel.Courses.Remove(Model.Course);
			}
			else
			{
				NameValue = "";
				CreditsValue = Model.AcademicPlan.CreditUnits.ToString();
				NumberValue = "";
				CategoryValue = "";
				SelectPrerequisiteCoursesModel.SelectedCourses = new List<Course>();
				SelectCommonlyOfferedInTermsModel.SelectedAcademicTerms = new List<AcademicTerm>();
			}

			CategoryOptions = new List<SelectListItem>();
			foreach (var vCat in Model.AcademicPlan.GetCategories())
			{
				CategoryOptions.Add(new SelectListItem
				{
					Text = vCat.Name,
					Value = vCat.EntityId.ToString(),
					Selected = (CategoryValue.Equals(vCat.EntityId.ToString()))
				});
			}
		}
	}
}